package android.medicalcloud.com.yiapp.yilian.ui.fragment;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.fragment.BaseFragment;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.ListUtil;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.logic.OrderController;
import android.medicalcloud.com.yiapp.yilian.model.OrderModel;
import android.medicalcloud.com.yiapp.yilian.model.OrdersModel;
import android.medicalcloud.com.yiapp.yilian.ui.view.RefreshLayout;
import android.medicalcloud.com.yiapp.yilian.ui.view.adapter.OderItemAdapter;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.io.Serializable;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class OrderItemFragment extends BaseFragment implements ViewNetCallBack{

    ListView listView;
    OderItemAdapter adapter ;
    RefreshLayout swiperefresh;

    int page=1;
    final int pageSize=10;
    View emptyView;

    @Override
    protected void initView(View view) {
        listView =(ListView) view.findViewById(R.id.lv);
        emptyView =view.findViewById(R.id.emptyView);
        swiperefresh =(RefreshLayout) view.findViewById(R.id.swiperefresh);
        swiperefresh.setColorSchemeResources(R.color.colorPrimary);
        swiperefresh.setmListView(listView);

        adapter =new OderItemAdapter(getContext());
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IntentTool.orderDetail(getContext(),adapter.getItem(position));
            }
        });
//        LayoutInflater inflater = LayoutInflater.from(getActivity());
//        View empView = inflater.inflate(R.layout.view_empty_lv, null);
//        ((ViewGroup) listView.getParent()).addView(empView, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
//        listView.setEmptyView(empView);

        swiperefresh.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                OrderController.getInstance().orderList(OrderItemFragment.this,state,page,pageSize);
            }
        });
        swiperefresh.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                Logger.e("down down  down");
                page=page+1;
                OrderController.getInstance().orderList(OrderItemFragment.this,state,page,pageSize);
            }
        });

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {


    }

    int state;
    @Override
    public void onResume() {
        super.onResume();
        state = getArguments().getInt(ARG_PAGE);
        Logger.e("state "+state);

        OrderController.getInstance().orderList(this,state,page,pageSize);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fra_order_type;
    }


    public static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;

    public static OrderItemFragment newInstance(int state) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, state);
        OrderItemFragment pageFragment = new OrderItemFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public void onConnectStart(Object o) {
        swiperefresh.setRefreshing(true);
    }

    @Override
    public void onConnectEnd() {
        if (swiperefresh == null) {
            return;
        }
        swiperefresh.setRefreshing(false);
    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {
        if (swiperefresh == null) {
            return;
        }
        swiperefresh.setLoading(false);
        swiperefresh.setHasMore(false);
    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        if (fromNet){
            if (swiperefresh == null) {
                return;
            }
            swiperefresh.setLoading(false);


            if (page==1){
                OrdersModel ordersModel = (OrdersModel)result;
                adapter.setRes(ordersModel.getOrders());
                swiperefresh.setLoading(false);
                swiperefresh.setHasMore(ordersModel.getOrders().size() >= pageSize);
                if (ListUtil.isNullOrEmpty(ordersModel.getOrders())){
                    emptyView.setVisibility(View.VISIBLE);
                }else{
                    emptyView.setVisibility(View.GONE);
                }
            }else{
                emptyView.setVisibility(View.GONE);
                OrdersModel ordersModel = (OrdersModel)result;
                adapter.addRes(ordersModel.getOrders());
                swiperefresh.setLoading(false);
                swiperefresh.setHasMore(ordersModel.getOrders().size() >= pageSize);
            }

        }

    }
}
